import * as Utils from "@/MCharts/utils";

function getCurrentSeries(optionsSeries, customSeries) {
  let result = optionsSeries;
  if (customSeries) {
    optionsSeries = { ...optionsSeries };
    customSeries = { ...customSeries };

    const seriesCollection = Utils.$m.mergeDeep(optionsSeries, customSeries);
    seriesCollection.length =
      Object.getOwnPropertyNames(seriesCollection).length;
    result = Array.from(seriesCollection);
  }

  return result;
}

function initOptions() {
  return {
    xAxis: {
      type: "category",
      boundaryGap: false,
      interval: 0,
      data: [],
    },
    yAxis: {
      type: "value",
    },
    series: [],
    grid: {
      left: "4%",
      right: "4%",
      bottom: "3%",
      containLabel: true,
    },
    legend: {
      data: [],
      right: "4%",
    },
    tooltip: {
      trigger: "axis",
      textAlign: "right",
    },
    animationEasing: "linear",
    animationDuration: 300,
    // animationEasingUpdate: 'linear',
    // animationDurationUpdate: 300,
  };
}

export default {
  options: {},
  // 获取配置项
  getOptions(data, customOptions, type) {
    this.options = initOptions();
    let options = this.options;
    let series = [];
    if (data && data.length) {
      data.forEach((e) => {
        let xAxisData = [];
        let seriesItem = {
          type: "line",
          areaStyle: {},
          smooth: true,
          ...e,
          name: e.name,
          data: [],
        };
        if (e.data && e.data.length) {
          e.data.forEach((el) => {
            xAxisData.push(el.name);
            seriesItem.data.push(el.value);
          });
          series.push(seriesItem);
        }
        options.legend.data.push(e.name);
        options.xAxis.data = xAxisData;
      });

      // options.series = series
      customOptions = { ...customOptions };
      customOptions.series = getCurrentSeries(series, customOptions.series);
    }

    console.log(Utils.$m.mergeDeep(options, customOptions));
    return Utils.$m.mergeDeep(options, customOptions);
  },
};
